<template >
  <div>
    <div  ><!-- v-if="this.rightcode.GwRight_List||this.loading " v-loading="loading"-->
      <el-container>
        <el-header style="border: 1px solid #CCC;">
          <!-- <el-button v-if="this.rightcode.GwRight_Insert" size="small" class="filter-item" type="primary" @click="AddRecord">登记角色 {{vrc=='true'?'(rightcode.GwRight_Insert)':''}}</el-button>
          <el-button v-if="this.rightcode.GwRight_Update" size="small" class="filter-item" type="primary" @click="Update">修改角色 {{vrc=='true'?'(rightcode.GwRight_Update)':''}}</el-button>
          <el-button v-if="this.rightcode.GwRight_Delete" size="small" class="filter-item" type="primary" :disabled="isDelRoleDisabled" @click="onDelRole">删除角色 {{vrc=='true'?'(rightcode.GwRight_Delete)':''}}</el-button> -->
          <el-button size="small" class="filter-item" type="primary" align="center" :disabled="savedisabled" :loading="Saving" @click="SaveUserRight">权限设置保存</el-button>
          <!-- <el-button v-if="this.rightcode.GwRight_SetRight" :disabled="this.disabled" size="small" class="filter-item" type="primary" :loading="Extracting" align="center" @click="ExtractMe">{{extText}}{{vrc=='true'?'(rightcode.GwRight_SetRight)':''}}</el-button> -->
        </el-header>
        <el-container>
          <el-aside style="border: 1px solid #CCC;">
            <view-tree :data="data" @node-click="TreeClick"></view-tree>
          </el-aside>
          <el-container>
            <el-main>
              <tabs-list :view-code="roleViewCode" @change="change" ref="tabsList"></tabs-list>
            </el-main>
          </el-container>
        </el-container>
      </el-container>
      <!-- <role-dialog ref="dlg" @Refresh="Refresh"></role-dialog> -->
    </div>
    <!-- <div v-else>
      <rigth401></rigth401>
    </div> -->
  </div>
</template>


<script>
import {
  getRoleListKeyInfo, saveUserRight
} from '@/api/user';
import viewTree from '@/components/BasicType/tree_view';
//import roleDialog from './comp_role_dialog';
import tabsList from './comp_tabs';
//import treeList from './comp_tree';
//import Rigth401 from '@/views/401';
//import { SetRightCode } from '@/api/user';
//import { mapGetters } from 'vuex';

export default {
  components: {
    viewTree,
    //roleDialog,
    tabsList,
    //treeList,
    //Rigth401,
  },
  data() {
    return {
      data: [],
      disabled: true,
      savedisabled:false,
      taskID:0,//后台计算可视会员范围的任务ID
      id: 0,
      loading: true,
      According: false,
      teep: {
        GwGroupID: 0,
        GwRightItemID: []
      },
      rightcode: {
        GwRight_List: false, //列表
        GwRight_Insert: false, //登记角色
        GwRight_Update: false, //修改角色
        GwRight_Delete: false, //删除角色
        GwRight_SetRight: false //保存权限
      },
      Saving:false,
      Extracting:false,
      extText:'重新计算会员范围',
      second:0,
      isDelRoleDisabled:true,
      roleViewCode:[] //角色可用可视码
    };
  },
  created() {
    this.GetRoleListKeyInfo();
    /* SetRightCode(this.rightcode).then(data=>{
      this.loading = false;
    }); */
    /* getRoleViewCode().then(r=>{
      if(r.errcode===0){
        this.roleViewCode=r.data;
      }
    }); 
    document.title = '角色设置 - 乐跳人才顾问版';//标题*/
  },
  computed: {
      /* ...mapGetters([
        'vrc',//显示权限代码    
      ]) */
    },
  methods: {
    GetRoleListKeyInfo() {
      getRoleListKeyInfo().then(rsp => {
        if (rsp.code === 0) {
          this.data = rsp.data;
        }
      });
    },

    AddRecord() {
      this.$refs.dlg.AddRecord(this.data);
    },
    Update() {
      if (this.id == 0) {
        this.$message.error('请选中一项');
        return;
      }
      this.$refs.dlg.UpdateRecord(this.id.toString());
    },
    onDelRole(){//删除无任何用户使用的角色
      delRole({role_id:this.id}).then(res=>{
        if(res.errcode==0){
          this.GetTreeData();
          this.$message.error('角色删除成功!');
        }
        else{
          this.$message.error('角色删除失败！原因：'+res.errmsg);
        }
      });
    },

    TreeClick(node) {
      this.According = true;
      setTimeout(_ => {
        this.$refs.tabsList.Thevalue(node.id);
      }, 300);
      this.id = node.id;
      this.teep.GwGroupID = node.id;
      console.log(this.id);
      //this.isDelRoleDisabled=node.UserCnt>0; //角色下有用户就不能删除
    },
    Refresh() {
      this.GetTreeData();
    },
    SaveUserRight() {
      if (this.id == 0) {
        this.$message.error('请选择一个角色组');
        return;
      }
      this.$confirm('确认保存当前角色的权限设置吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{        
        this.Saving=true;
        this.$refs.tabsList.call();
        setTimeout(_ => {
          console.log('this.teep',this.teep);
          saveUserRight(this.teep).then(rsp => {
            console.log(rsp);
            if (rsp.code === 0) {
              this.disabled = false;
              this.$message.success('保存成功');
              this.Saving=false;
            } else {
              this.$message.error('保存失败！原因：'+rsp.errmsg);
              this.Saving=false;
            }
          });
        }, 1000);
      });
    },
    change(value) {
      this.teep.GwRightItemID = value;
    },
  }
};
</script>
<style scoped>
.el-header {
    line-height: 60px;
}

.el-aside {
    text-align: center;
}

.el-main {
    padding: 0px;
}
</style>
